package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.AdminService;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = AdminService.class)
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private UserDao userDao;


    public void update(Map map, String name) {
        User byUsername = userDao.findByUsername(name);
        BCryptPasswordEncoder enc = new BCryptPasswordEncoder();
//        准备旧密码
        String j = (String) map.get("j");
//        准备数据库中的密码
        String password = byUsername.getPassword();
        boolean matches = enc.matches(j,password);
        if (map.get("x").equals( map.get("q"))) {
            if (!matches) {
                throw new RuntimeException("密码错误不可以修改");
            } else {
                String x = (String) map.get("x");


                String aa = enc.encode(x);
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("password", aa);
                hashMap.put("name", name);
                userDao.set(hashMap);

            }
        } else {
            throw new RuntimeException("密码不一致");
        }




    }

    public static void main(String[] args) {
//        StandardPasswordEncoder pawword = new StandardPasswordEncoder();
//            boolean matches = pawword.matches("1234", "$10$3xW2nBjwBM3rx1LoYprVsemNri5bvxeOd/QfmO7UDFQhW2HRHLi.C");
//        System.out.println(matches);

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean matches = encoder.matches("1234", "$2a$10$oMhOKVZCyTN63luMMxv62uSVKtGfHqWoWtC5JprWOmWpNkxIK03Vm");
//        String encode = encoder.encode("1234");

        System.out.println(matches);
    }


}
